package com.captain.special.数组;

/**
 * Des:
 *
 * @author XL
 * @Date 2022/4/4 10:34
 */
public class 移除元素27 {
    public static void main(String[] args) {
        System.out.println(new 移除元素27().removeElement(new int[]{2}, 3));
    }
    //双指针
    // 1 2 2 3 4 5   2
    public int removeElement(int[] nums, int val) {
        int slow = 0;
        //1 2 2 3 3 4
        for (int i = 0; i < nums.length; i++) {
            int curVal = nums[i];
            //当前值不相等
            if (val != curVal) {
                //交换元素
                nums[slow] = nums[i];
                slow++;
            }
            //如果相等,fast继续向下循环
        }
        return slow;
    }
}
